package com.only4play.httpapt.support.api.impl;

import com.only4play.httpapt.common.http.HttpType;
import com.only4play.httpapt.common.http.apache.ApacheHttpClientBuilder;
import com.only4play.httpapt.common.http.apache.DefaultApacheHttpClientBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.HttpHost;

/**
 * @author liyuncong
 * @version 1.0
 * @file SimpleHttpSupportApacheHttpClientImpl
 * @brief SimpleHttpSupportApacheHttpClientImpl
 * @details SimpleHttpSupportApacheHttpClientImpl
 * @date 2024-08-16
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-08-16               liyuncong          Created
 */
public class SimpleHttpSupportApacheHttpClientImpl extends BaseSimpleHttpSupportImpl<CloseableHttpClient, HttpHost> {

    private CloseableHttpClient httpClient;
    private HttpHost httpHost;

    @Override
    public CloseableHttpClient getRequestHttpClient() {
        return httpClient;
    }

    @Override
    public HttpHost getRequestHttpProxy() {
        return httpHost;
    }

    @Override
    public HttpType getRequestType() {
        return HttpType.APACHE_HTTP;
    }

    @Override
    public void initHttp() {
        ApacheHttpClientBuilder apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.builder();
        if (httpHost != null) {
            httpClient = apacheHttpClientBuilder
                .httpProxyHost(httpHost.getHostName())
                .httpProxyPort(httpHost.getPort())
                .build();
        } else {
            httpClient = apacheHttpClientBuilder.build();
        }
    }
}
